package
{
	import events.NavigationEvent;
	
	import flash.desktop.NativeApplication;
	import flash.events.KeyboardEvent;
	import flash.media.SoundMixer;
	import flash.ui.Keyboard;
	
	import screens.ExitGame;
	import screens.GameOver;
	import screens.InGame;
	import screens.Welcome;
	
	import starling.core.Starling;
	import starling.display.Button;
	import starling.display.Image;
	import starling.display.MovieClip;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.TouchEvent;
	
	public class Game extends Sprite
	{
		private var screenWelcome:Welcome;
		private var screenInGame:InGame;

		private var img:Image;
		private var screenOver:GameOver;
		private var sndOn:MovieClip;
		private var sndOff:Button;
		private var exitGame:ExitGame;
		
		public function Game()
		{
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}

		private function onAddedToStage(event:Event):void
		{
			
			HungryHeroMobile.txt.visible = false;
			
			this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(events.NavigationEvent.CHANGE_SCREEN, onChangeScreen);
			
			screenInGame = new InGame();
			screenInGame.disposeTemporarily();
			this.addChild(screenInGame);
			
			screenWelcome = new Welcome();
			this.addChild(screenWelcome);
			screenWelcome.initialize();
			
			this.screenOver = new GameOver();
			this.addChild(screenOver);
			
			GameSound.getInstance().init();
			
			this.exitGame = new ExitGame();
			this.addChild(exitGame);
			
			NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN,backHandler);
			
			creatSoundBtn();
			
		}
		/**
		 *返回键 
		 * @param event
		 * 
		 */		
		protected function backHandler(event:KeyboardEvent):void
		{
			if(event.keyCode == Keyboard.MENU || event.keyCode==Keyboard.BACK|| event.keyCode==Keyboard.SEARCH){
				event.preventDefault();
				exitGame.visible = true;
			}
		}
		
		private function creatSoundBtn():void{
			
			this.sndOn = new MovieClip(Assets.getAtlas().getTextures("soundOn"),4);
			this.sndOff = new Button(Assets.getAtlas().getTexture("soundOff"));
			this.sndOn.x = this.sndOff.x= 40;
			this.sndOn.y = this.sndOff.y= stage.stageHeight-40-this.sndOff.textBounds.height*0.5;
			
			sndOn.touchable = true;
			Starling.juggler.add(sndOn);
			this.sndOn.addEventListener(TouchEvent.TOUCH,sndOnOff);
			
			this.addChild(sndOff);
			this.addChild(sndOn);
			sndOn.visible = false;
			
			this.sndOff.addEventListener(TouchEvent.TOUCH,sndOnOff);
		}
		
		private function sndOnOff(e:Event):void
		{
			if(e.currentTarget == sndOn){
				this.sndOff.visible = true;
				this.sndOn.visible = false;
				GameSound.getInstance().setBgVol(0);
			}else{
				this.sndOff.visible = false;
				this.sndOn.visible = true;
				GameSound.getInstance().setBgVol(0.8);
			}
		}
		
		private function onChangeScreen(event:NavigationEvent):void
		{
			switch (event.params.id)
			{
				case "main":
					screenWelcome.initialize();
					screenOver.disposeTemporarily();
					screenInGame.disposeTemporarily();
					break;
				case "play":
					screenWelcome.disposeTemporarily();
					screenInGame.initialize();
					screenOver.disposeTemporarily();
					break;
				case "over":
					screenInGame.disposeTemporarily();
					screenWelcome.disposeTemporarily();
					screenOver.initialize(event.params);
					break;
			}
		}
	}
}